<?php
namespace WebApper;


abstract class Notification extends Base {

	/**
	 * The notification id
	 *
	 * @var integer
	 */
	var $id;

	/**
	 * The post ID the notification is related to
	 *
	 * @var integer
	 */
	var $item_id;

	/**
	 * The type of notification
	 *
	 * @var string
	 */
	var $type;

	/**
	 * The message that will be displayed in the notification menu
	 *
	 * @var string
	 */
	var $short_message;

	/**
	 * The message that will be displayed on the notifications page
	 *
	 * @var string
	 */
	var $long_message;

	/**
	 * The user ID for who the notification is for.
	 *
	 * @var integer
	 */
	var $user_id;

	/**
	 * Is the notification new or has it already been read.
	 *
	 * @var boolean
	 */
	var $is_new;

	/**
	 * The date the notification was seen and no longer new.
	 *
	 * @var string
	 */
	var $notified_on;

	/**
	 * The date the notification was created.
	 *
	 * @var string
	 */
	var $created_on;

	/** Public Methods ********************************************************/

	/**
	 * Constructor
	 *
	 * @param integer $id
	 */
	function __construct( $id = 0 ) {
		if ( !empty( $id ) ) {
			$this->id = $id;
			$this->populate();
		}
	}

/**
 * Update or insert notification details into the database.
 *
 * @global wpdb $wpdb WordPress database object
 * @return bool Success or failure
 */
function save() {
	global $wpdb;

	// Update
	if ( !empty( $this->id ) ) {
		$sql = $wpdb->prepare( "UPDATE {$webapper->tables->notifications} SET item_id = %d, type = %s, short_message = %d, long_message = %s, user_id = %d, is_new = %d, notified_on = %d, created_on = %d ) WHERE ID = %d", $this->item_id, $this->type, $this->short_message, $this->long_message, $this->user_id, $this->is_new, $this->notified_on, $this->created_on, $this->id );

	// Save
	} else {
		$sql = $wpdb->prepare( "INSERT INTO {$webapper->tables->notifications} ( item_id, type, short_message, long_message, user_id, is_new, notified_on, created_on ) VALUES ( %d, %s, %s, %s, %d, %d, %d, %d )", $this->item_id, $this->type, $this->short_message, $this->long_message, $this->user_id, $this->is_new, $this->notified_on, $this->created_on );
	}

	if ( !$result = $wpdb->query( $sql ) )
		return false;

	$this->id = $wpdb->insert_id;

	return true;
}

	/** Private Methods *******************************************************/

	/**
	 * Fetches the notification data from the database.
	 *
	 * @global $webapper
	 * @global wpdb $wpdb WordPress database object
	 */
	private function populate() {
		global $webapper, $wpdb;
		if ( $notification = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$webapper->tables->notifications} WHERE ID = %d", $this->id ) ) ) {
			$this->type				= $notification->type;
			$this->short_message	= $notification->short_message;
			$this->long_message		= $notification->long_message;
			$this->user_id			= $notification->user_id;
			$this->is_new			= $notification->is_new;
			$this->notified_on		= $notification->notified_on;
			$this->created_on		= $notification->created_on;
		}
	}

	/** Static Methods ********************************************************/

	/**
	 * Fetches all the notifications in the database for a specific user.
	 *
	 * @global $webapper
	 * @global wpdb $wpdb WordPress database object
	 * @param integer $user_id User ID
	 * @param bool $is_new
	 * @return array Associative array
	 * @static
	 */
	function get_all_for_user( $user_id, $is_new = true ) {
		global $webapper, $wpdb;
		$is_new = ( $is_new === true ) ? ' AND is_new = 1 ' : '';
 		return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM  WHERE user_id = %d {$is_new}", $user_id ) );
	}

	/**
	 * Delete all the notifications for a user based on the component name and action.
	 *
	 * @global $webapper
	 * @global wpdb $wpdb WordPress database object
	 * @param integer $user_id
	 * @param string $type
	 * @static
	 */
//	function delete_for_user_by_type( $user_id, $type ) {
//		global $webapper, $wpdb;
//		return $wpdb->query( $wpdb->prepare( "DELETE FROM {$webapper->tables->notifications} WHERE user_id = %d AND type = %s", $user_id, $type ) );
//	}

	/**
	 * Delete notification by ID.
	 *
	 * @global $webapper
	 * @global wpdb $wpdb WordPress database object
	 * @param integer $ID The ID of the notification.
	 * @static
	 */
//	function delete_for_user_by_item_id( $ID ) {
//		global $webapper, $wpdb;
//		return $wpdb->query( $wpdb->prepare( "DELETE FROM {$webapper->tables->notifications} WHERE ID = %d", $ID ) );
//	}

}

?>